#include <stdio.h>
#include "ulog.h"
#include "memheap.h"

void my_console_logger(ulog_level_t severity, char *msg)
{
	if (severity >= ULOG_TRACE_LEVEL && severity <= ULOG_ALWAYS_LEVEL)
	{
		printf("[%-8s][%s:%d]:%s\n", ulog_level_name(severity), __FILE__, __LINE__, msg);
	}
	else
	{
		printf(msg);
	}
	fflush(stdout);
}

int main()
{
	ULOG_INIT();

	ULOG_SUBSCRIBE(my_console_logger, ULOG_DEBUG_LEVEL);
//  ULOG_SUBSCRIBE(my_console_logger, ULOG_INFO_LEVEL);
//  ULOG_SUBSCRIBE(my_console_logger, ULOG_WARNING_LEVEL);
//  ULOG_SUBSCRIBE(my_console_logger, ULOG_ERROR_LEVEL);
//  ULOG_SUBSCRIBE(my_console_logger, ULOG_CRITICAL_LEVEL);

	uint32_t used = 0, total = 0, max_used = 0;
	char* p = NULL;
	char* p2 = NULL;

	memheap_init();

	memheap_info(&total, &used, &max_used);
	ULOG_INFO(" total:%d, used:%d, max_used:%d ", total, used, max_used);

	p = memheap_malloc(100);
	ULOG_INFO(" memheap_malloc %s !", p == NULL ? "failed" : "success");

	memheap_info(&total, &used, &max_used);
	ULOG_INFO(" total:%d, used:%d, max_used:%d ", total, used, max_used);

	if(p)
	{
		sprintf(p, " hello memheap !");
		ULOG_INFO("%s ", p);
	}

	p2 = memheap_malloc(200);

	memheap_info(&total, &used, &max_used);
	ULOG_INFO(" total:%d, used:%d, max_used:%d ", total, used, max_used);

	memheap_free(p2);
	memheap_info(&total, &used, &max_used);
	ULOG_INFO(" total:%d, used:%d, max_used:%d ", total, used, max_used);

	memheap_free(p);

	memheap_info(&total, &used, &max_used);
	ULOG_INFO(" total:%d, used:%d, max_used:%d ", total, used, max_used);
	return 0;
}
/*
[INFO    ][..\src\main.c:9]: total:8160, used:0, max_used:0 
[INFO    ][..\src\main.c:9]: memheap_malloc success !
[INFO    ][..\src\main.c:9]: total:8160, used:116, max_used:116 
[INFO    ][..\src\main.c:9]: hello memheap ! 
[INFO    ][..\src\main.c:9]: total:8160, used:332, max_used:332 
[INFO    ][..\src\main.c:9]: total:8160, used:116, max_used:332 
[INFO    ][..\src\main.c:9]: total:8160, used:0, max_used:332 

*/
